
import Toast from '@vant/weapp/toast/toast';
const app = getApp()
const { default: toast } = require('@vant/weapp/toast/toast.js');
const api = app.globalData.api

Page({

  /**
   * 页面的初始数据
   */
  data: {
    info:{},
    id:'',
    isBack:false,
    commit_name_id:'',
    commit_name:'',
    ico:'',
    hallId:'',
    time_popup_show:false,
    type_popup_show:false,
    typeOption:[],
    recordDate:'',
    currentTimestamp:'',
    mealType_selected:'',
    nowType:'',
    menu:[],
  },
  
  /**
   * 选择餐别
   */
  choosePayType(e){
    let type = e.currentTarget.dataset.type;
    let that = this;
    api.curlPost('?cla=canteenApp&fun=replaceReportSearch').then((res)=>{
        if(type == 'hall'){
          that.setData({
            nowType:type,
            type_popup_show:true,
            typeOption:res.data.hallOption
          })
        }else if(type =='mealType'){
          console.log(res.data.mealOption)
          that.setData({
            nowType:type,
            type_popup_show:true,
            typeOption:res.data.mealOption
          })
        }
      })
  },
  chooseStaff(e){
    wx.navigateTo({
      url: '../staff/staff',
    })
  },
  /**餐别确认 */
  onTypeConfirm(event){
    const { picker, value, index } = event.detail;
    if(this.data.nowType =='hall'){
      console.log(value);
      this.setData({
        hallName:value.name,
        hallId:value.id,
        type_popup_show:false
      })
    
    }else if(this.data.nowType == 'mealType'){
      this.setData({
        mealType_selected:value,
        type_popup_show:false
      })
    }

    this.getMenu();
  },
  onTypeCancel(e){
    this.setData({
      type_popup_show:false
    })
  },
  commit(e){
    let para = e.detail.value;
    para.id = this.data.id;
    para.date = this.data.recordDate;
    para.staffId = this.data.commit_name_id;
    para.hallId = this.data.hallId;
    para.menu = JSON.stringify(this.data.menu);
    para.type='代报餐';
    api.curlPost('?cla=canteenApp&fun=reportEdit',para).then((res)=>{
      Toast(res.warn);
       wx.navigateBack({
         delta: 1,
       })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // let id = options.id;
    let that = this;

    api.curlPost('?cla=canteenApp&fun=replaceReportSearch',{}).then((res)=>{
        that.setData({
          typeOption:res.data.mealOption,
        })
      })
    
  },

  //获取菜单
  getMenu(){
    let that = this;
    let para = {
      hallId:this.data.hallId,
      recordDate:this.data.recordDate,
      mealType:this.data.mealType_selected,
     
    }
   
    if(api.isBlank(para.hallId) || api.isBlank(para.recordDate) || api.isBlank(para.mealType)){
      console.log(para)
      return false;
    }
    api.curlPost('?cla=canteenApp&fun=getMenu',para).then((res)=>{
      that.setData({
        menu:res.data.menu
      })
    })

  },

  //步进器
  onStepChange(event){
    let num = event.detail;
    let id = event.currentTarget.dataset.id;
    let arr = this.data.menu;
    this.data.menu.forEach((v,k) => {
      if(v.id == id){
        arr[k].num = num;
      }
    });
    this.setData({
      menu:arr
    })
  },
 
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    if(this.data.isBack){
      console.log(this.data.commit_name)
      this.setData({
        isBack:false,
        staffName:this.data.commit_name
      });
      api.curlPost('?cla=canteenApp&fun=getCanteen',{staffId:this.data.commit_name_id}).then((res)=>{
        that.setData({
          canteen:res.data.canteen,
          hallId:res.data.hallId
        })
      })
      this.getMenu();
    }
  },
/**选择时间 */
chooseTime(e){
  let currentTimestamp = this.data.recordDate?new Date(api.rowTocol(this.data.recordDate)).getTime():new Date().getTime();
  this.setData({
    time_popup_show:true,
    currentTimestamp:currentTimestamp
  })

},
/**日期选择确认 */
timeConfirm(event){

  let date = new Date(event.detail);
    this.setData({
      recordDate: api.formatDateCRM('Y-m-d',date),
      time_popup_show:false,
    })
    this.getMenu();
},
timeCancel(e){
  this.setData({
    time_popup_show:false
  })
},
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})